<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>box.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_box.js.html">box.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for representing a numeric box.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.math.Box&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Class for representing a box. A box is specified as a top, right, bottom,
<a name="line28"></a> * and left. A box is useful for representing margins and padding.
<a name="line29"></a> *
<a name="line30"></a> * @param {number} top Top.
<a name="line31"></a> * @param {number} right Right.
<a name="line32"></a> * @param {number} bottom Bottom.
<a name="line33"></a> * @param {number} left Left.
<a name="line34"></a> * @constructor
<a name="line35"></a> */
<a name="line36"></a>goog.math.Box = function(top, right, bottom, left) {
<a name="line37"></a>  /**
<a name="line38"></a>   * Top
<a name="line39"></a>   * @type {number}
<a name="line40"></a>   */
<a name="line41"></a>  this.top = top;
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * Right
<a name="line45"></a>   * @type {number}
<a name="line46"></a>   */
<a name="line47"></a>  this.right = right;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * Bottom
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   */
<a name="line53"></a>  this.bottom = bottom;
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Left
<a name="line57"></a>   * @type {number}
<a name="line58"></a>   */
<a name="line59"></a>  this.left = left;
<a name="line60"></a>};
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Creates a Box by bounding a collection of goog.math.Coordinate objects
<a name="line65"></a> * @param {goog.math.Coordinate} var_args Coordinates to be included inside the
<a name="line66"></a> *     box.
<a name="line67"></a> * @return {goog.math.Box} A Box containing all the specified Coordinates.
<a name="line68"></a> */
<a name="line69"></a>goog.math.Box.boundingBox = function(var_args) {
<a name="line70"></a>  var box = new goog.math.Box(arguments[0].y, arguments[0].x,
<a name="line71"></a>                              arguments[0].y, arguments[0].x);
<a name="line72"></a>  for (var i = 1; i &lt; arguments.length; i++) {
<a name="line73"></a>    var coord = arguments[i];
<a name="line74"></a>    box.top = Math.min(box.top, coord.y);
<a name="line75"></a>    box.right = Math.max(box.right, coord.x);
<a name="line76"></a>    box.bottom = Math.max(box.bottom, coord.y);
<a name="line77"></a>    box.left = Math.min(box.left, coord.x);
<a name="line78"></a>  }
<a name="line79"></a>  return box;
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Creates a copy of the box with the same dimensions.
<a name="line85"></a> * @return {goog.math.Box} A clone of this Box.
<a name="line86"></a> */
<a name="line87"></a>goog.math.Box.prototype.clone = function() {
<a name="line88"></a>  return new goog.math.Box(this.top, this.right, this.bottom, this.left);
<a name="line89"></a>};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>if (goog.DEBUG) {
<a name="line93"></a>  /**
<a name="line94"></a>   * Returns a nice string representing the box.
<a name="line95"></a>   * @return {string} In the form (50t, 73r, 24b, 13l).
<a name="line96"></a>   */
<a name="line97"></a>  goog.math.Box.prototype.toString = function() {
<a name="line98"></a>    return &#39;(&#39; + this.top + &#39;t, &#39; + this.right + &#39;r, &#39; + this.bottom + &#39;b, &#39; +
<a name="line99"></a>           this.left + &#39;l)&#39;;
<a name="line100"></a>  };
<a name="line101"></a>}
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Returns whether the box contains a coordinate or another box.
<a name="line106"></a> *
<a name="line107"></a> * @param {goog.math.Coordinate|goog.math.Box} other A Coordinate or a Box.
<a name="line108"></a> * @return {boolean} Whether the box contains the coordinate or other box.
<a name="line109"></a> */
<a name="line110"></a>goog.math.Box.prototype.contains = function(other) {
<a name="line111"></a>  return goog.math.Box.contains(this, other);
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Expands box with the given margins.
<a name="line117"></a> *
<a name="line118"></a> * @param {number|goog.math.Box} top Top margin or box with all margins.
<a name="line119"></a> * @param {number} opt_right Right margin.
<a name="line120"></a> * @param {number} opt_bottom Bottom margin.
<a name="line121"></a> * @param {number} opt_left Left margin.
<a name="line122"></a> * @return {goog.math.Box} A reference to this Box.
<a name="line123"></a> */
<a name="line124"></a>goog.math.Box.prototype.expand = function(top, opt_right, opt_bottom,
<a name="line125"></a>    opt_left) {
<a name="line126"></a>  if (goog.isObject(top)) {
<a name="line127"></a>    this.top -= top.top;
<a name="line128"></a>    this.right += top.right;
<a name="line129"></a>    this.bottom += top.bottom;
<a name="line130"></a>    this.left -= top.left;
<a name="line131"></a>  } else {
<a name="line132"></a>    this.top -= top;
<a name="line133"></a>    this.right += opt_right;
<a name="line134"></a>    this.bottom += opt_bottom;
<a name="line135"></a>    this.left -= opt_left;
<a name="line136"></a>  }
<a name="line137"></a>
<a name="line138"></a>  return this;
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Compares boxes for equality.
<a name="line144"></a> * @param {goog.math.Box} a A Box.
<a name="line145"></a> * @param {goog.math.Box} b A Box.
<a name="line146"></a> * @return {boolean} True iff the boxes are equal, or if both are null.
<a name="line147"></a> */
<a name="line148"></a>goog.math.Box.equals = function(a, b) {
<a name="line149"></a>  if (a == b) {
<a name="line150"></a>    return true;
<a name="line151"></a>  }
<a name="line152"></a>  if (!a || !b) {
<a name="line153"></a>    return false;
<a name="line154"></a>  }
<a name="line155"></a>  return a.top == b.top &amp;&amp; a.right == b.right &amp;&amp;
<a name="line156"></a>         a.bottom == b.bottom &amp;&amp; a.left == b.left;
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Returns whether a box contains a coordinate.
<a name="line162"></a> *
<a name="line163"></a> * @param {goog.math.Box} box A Box.
<a name="line164"></a> * @param {goog.math.Coordinate|goog.math.Box} other A Coordinate or a Box.
<a name="line165"></a> * @return {boolean} Whether the box contains the coordinate or other box.
<a name="line166"></a> */
<a name="line167"></a>goog.math.Box.contains = function(box, other) {
<a name="line168"></a>  if (!box || !other) {
<a name="line169"></a>    return false;
<a name="line170"></a>  }
<a name="line171"></a>
<a name="line172"></a>  if (other instanceof goog.math.Box) {
<a name="line173"></a>    return other.left &gt;= box.left &amp;&amp; other.right &lt;= box.right &amp;&amp;
<a name="line174"></a>      other.top &gt;= box.top &amp;&amp; other.bottom &lt;= box.bottom;
<a name="line175"></a>  }
<a name="line176"></a>
<a name="line177"></a>  // other is a Coordinate.
<a name="line178"></a>  return other.x &gt;= box.left &amp;&amp; other.x &lt;= box.right &amp;&amp;
<a name="line179"></a>         other.y &gt;= box.top &amp;&amp; other.y &lt;= box.bottom;
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Returns the distance between a coordinate and the nearest corner/side of a
<a name="line185"></a> * box. Returns zero if the coordinate is inside the box.
<a name="line186"></a> *
<a name="line187"></a> * @param {goog.math.Box} box A Box.
<a name="line188"></a> * @param {goog.math.Coordinate} coord A Coordinate.
<a name="line189"></a> * @return {number} The distance between {@code coord} and the nearest
<a name="line190"></a> *     corner/side of {@code box}, or zero if {@code coord} is inside
<a name="line191"></a> *     {@code box}.
<a name="line192"></a> */
<a name="line193"></a>goog.math.Box.distance = function(box, coord) {
<a name="line194"></a>  if (coord.x &gt;= box.left &amp;&amp; coord.x &lt;= box.right) {
<a name="line195"></a>    if (coord.y &gt;= box.top &amp;&amp; coord.y &lt;= box.bottom) {
<a name="line196"></a>      return 0;
<a name="line197"></a>    }
<a name="line198"></a>    return coord.y &lt; box.top ? box.top - coord.y : coord.y - box.bottom;
<a name="line199"></a>  }
<a name="line200"></a>
<a name="line201"></a>  if (coord.y &gt;= box.top &amp;&amp; coord.y &lt;= box.bottom) {
<a name="line202"></a>    return coord.x &lt; box.left ? box.left - coord.x : coord.x - box.right;
<a name="line203"></a>  }
<a name="line204"></a>
<a name="line205"></a>  return goog.math.Coordinate.distance(coord,
<a name="line206"></a>      new goog.math.Coordinate(coord.x &lt; box.left ? box.left : box.right,
<a name="line207"></a>                               coord.y &lt; box.top ? box.top : box.bottom));
<a name="line208"></a>};
</pre>


</body>
</html>
